<?php
require_once APP . '/application/admincontrollers/AdminAction.php';
require_once(APP . '/application/models/Alllist.php');

/**
 * 后台管理主页
 * @package admin
 */
class ZxgkController extends AdminAction
{
    
	/**
	 * Enter description here...
	 *
	 * @var Article
	 */
	private $article;
    public function init()
    {
    	require_once(APP . '/application/models/Article.php');
    	
        $this->article = new Article($this->db);
    }


    /**
     * 加载框架页
     */
    public function addAction()
    {
    	$type = $this->getParam("type");
    	
        //加载默认视图
        $option = $this->create($type);
        $this->view->option = $option;
        $this->view->type = $type;
    }
    
    /**
     * 创建分类
     *
     * @param unknown_type $type
     * @param unknown_type $default
     * @return unknown
     */
    private function create($type, $default=0)
    {
    	global $cfg;
    	
    	$obj_list = new Alllist($this->db);

    	$result = '';
    	$arr = $obj_list->getAllListByType($cfg['zxgk_list_type']);
    	
    	foreach ($arr as $v){
    		$select = '';
    		
    		// 如果叶子节点已经有关联文章，则忽略
    		if ($v['isleaf'] && !empty($v['leafid'])){
    			$exist = $this->db->fetchOne("select count(*) from article where aid=?", array($v['leafid']));
    			if ($exist)
    				continue;
    		}
    		
    		$result .= '<option value="' . $v['id'] . '" ' . $select . '>' . $v['name'] . '</option>';
    	}
    	
    	return $result;
    }

    /**
     * 发布新闻
     *
     */
    public function doaddAction()
    {
    	$cid = (int) $this->getParam("cid");
    	$author = $this->getParam("author");
    	$title = $this->getParam("title");
    	$content = $this->getParam("content");
    	$pubtime = $this->getParam("pubtime");
    	$seq = (int) $this->getParam("seq");
    	$afrom = $this->getParam("afrom");
    	$keywords = $this->getParam("keywords");
    	$subtitle = $this->getParam("subtitle");
    	
    	if ($this->getParam("top") == 1){
    		$seq = $this->db->fetchOne("select max(seq)+1 from article");
    	}
    	
    	$obj_list = new Alllist($this->db);
    	$arr_list_info = $obj_list->getDetailByID($cid);
    	if ($arr_list_info['isleaf'] && !empty($arr_list_info['leafid'])){
    		$exist = $this->db->fetchOne("select count(*) from article where aid=?", array($arr_list_info['leafid']));
    		if ($exist){
		    	$this->view->msg = '该分类为叶子分类，不能添加介绍！';
		    		
		    	$this->view->url = $this->url($this->view->baseUrl, "zxgk", "add", "type=" . $type, "admin");
		    	$this->showmessage();
		    	exit;
    		}
    	}
    	
    	$aid = $this->article->add($cid, $author, $title,$subtitle, $content, $pubtime, $seq, $afrom, $keywords, $image);
    	if ($arr_list_info['isleaf']){
    		$obj_list->change($cid, array("leafid" => $aid));
    	}
    	
    	if ($type == 'center')
    		$this->view->msg = '添加成功！';
    	else 
    		$this->view->msg = '添加成功';
    		
    	$this->view->url = $this->url($this->view->baseUrl, "zxgk", "add", "type=" . $type, "admin");
    	$this->showmessage();
    }
    
    /**
     * 文章管理
     *
     */
    public function listAction()
    {
    	global $cfg;
    	$cid = (int) $this->getParam("cid");
    	$nowp = (int) $this->getParam("nowp", 1);
    	$type = $this->getParam("type");
    	$per_page = 20;
    	
    	$arr_types = array();
    	$arr_cid = array();
		
		$typeid = $cfg['zxgk_list_type'];
		
		$obj_list = new Alllist($this->db);
		$arr = $obj_list->getAllListByType($typeid);
		
		foreach ($arr as $v){
			$arr_cid[] = $v['id'];
			$arr_types[$v['id']] = $v['name'];
		}
		$where = ' where cid in (' . implode(',', $arr_cid) . ')';
		
		$result = $this->article->listArticleByWhere($where, $nowp, $per_page);
    	
    	$this->view->nowp = $nowp;
        $this->view->list = $result['list'];
        $this->view->page = $result['page'];
        $this->view->type = $type;
        $this->view->types = $arr_types;
    }
        
    public function delAction()
    {
    	$aid = (int) $this->getParam("aid");
    	$nowp = (int) $this->getParam("nowp");
    	$type = $this->getParam("type");
    	
    	$this->article->del($aid);
    	
    	header("location:" . $this->url($this->view->baseUrl, "zxgk", "list", "nowp=" . $nowp . '&type='. $type, "admin"));
    }
    
    public function editAction()
    {
    	$aid = (int) $this->getParam("aid");
    	$nowp = (int) $this->getParam("nowp");
    	$type = $this->getParam("type");
    	$content = $this->article->show($aid);
        $option = $this->create('study', $content['cid']);
        
        $this->view->nowp = $nowp;
        $this->view->content = $content;
        $this->view->option = $option;
        $this->view->type = $type;
    }
    
    public function doeditAction()
    {
    	$aid = (int)$this->getParam("aid");
    	$cid = (int) $this->getParam("cid");
    	$author = $this->getParam("author");
    	$title = $this->getParam("title");
    	$content = $this->getParam("content");
    	$pubtime = $this->getParam("pubtime");
    	$seq = (int) $this->getParam("seq");
    	$afrom = $this->getParam("afrom");
    	$keywords = $this->getParam("keywords");
    	$delimage = $this->getParam("delimage");
    	if ($this->getParam("top") == 1){
    		$seq = $this->db->fetchOne("select max(seq)+1 from article");
    	}
    	
    	$nowp = $this->getParam("nowp");
    	
    	$v['title'] = $title;
    	$v['subtitle'] = $this->getParam("subtitle");
    	$v['content'] = $content;
    	$v['seq'] = $seq;
    	
    	$this->article->change($aid, $v);
    	
    	$this->view->msg = '编辑成功';
    	$this->view->url = $this->url($this->view->baseUrl, "zxgk", "list", "nowp=" . $nowp . '&type=' . $this->getParam("type"), "admin");
    	$this->showmessage();
    }
}